package com.wisewin.circle.api.controller;

import com.wisewin.circle.api.controller.base.BaseController;
import com.wisewin.circle.config.dto.ResultDTO;
import com.wisewin.circle.config.dto.ResultDTOBuilder;
import com.wisewin.circle.entity.LoginUser;
import com.wisewin.circle.entity.bo.QueryParamBO;
import com.wisewin.circle.entity.db.ActivityCommentDO;
import com.wisewin.circle.entity.db.ActivityCommentHeartDO;
import com.wisewin.circle.entity.db.ActivityDO;
import com.wisewin.circle.entity.db.OrgUserDO;
import com.wisewin.circle.service.ActivityService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/activity")
public class ActivityController extends BaseController {

    @Autowired
    private ActivityService activityService;

    // 创建活动上传图片
    @PostMapping("/uploadActivityHeadUrl")
    public ResultDTO uploadActivityHeadUrl (HttpServletRequest request, MultipartFile[] lstFile) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        Integer userId = null;
        // 获取登录用户
        if(token.startsWith("user_")) {
            LoginUser loginUser = super.getLoginUser(token);
            if (loginUser == null) { // 判断是否登录
                return ResultDTOBuilder.failure("0000001", "登录已过期", null);
            }
            userId = loginUser.getId();
        }else if(token.startsWith("orguser_")){
            // 获取登录用户
            OrgUserDO orgUserDO = super.getOrgLoginUser(token);
            if (orgUserDO == null ) { // 判断是否登录
                return ResultDTOBuilder.failure("0000001", "登录已过期", null);
            }
            userId = orgUserDO.getCreateUserId();
        }
        return activityService.uploadFile(lstFile, userId, "activityHeadUrl");
    }

    // 创建活动
    @PostMapping("/addActivity")
    public ResultDTO addActivity (HttpServletRequest request, @RequestBody ActivityDO activityDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        if(token.startsWith("user_")) {
            LoginUser loginUser = super.getLoginUser(token);
            if (loginUser == null) { // 判断是否登录
                return ResultDTOBuilder.failure("0000001", "登录已过期", null);
            }
            activityDO.setUserId(loginUser.getId());
        }else if(token.startsWith("orguser_")){
            // 获取登录用户
            OrgUserDO orgUserDO = super.getOrgLoginUser(token);
            if (orgUserDO == null ) { // 判断是否登录
                return ResultDTOBuilder.failure("0000001", "登录已过期", null);
            }
            activityDO.setUserId(orgUserDO.getCreateUserId());
        }
        return activityService.addActivity(activityDO);
    }

    // 修改活动信息
    @PostMapping("/updateActivity")
    public ResultDTO updateActivity (HttpServletRequest request, @RequestBody ActivityDO activityDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        OrgUserDO orgUserDO = super.getOrgLoginUser(token);
        if (orgUserDO == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        return activityService.updateActivity(activityDO);
    }

    // 删除活动
    @PostMapping("/deleteActivity")
    public ResultDTO deleteActivity (HttpServletRequest request, @RequestBody ActivityDO activityDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        if(token.startsWith("user_")) {
            LoginUser loginUser = super.getLoginUser(token);
            if (loginUser == null) { // 判断是否登录
                return ResultDTOBuilder.failure("0000001", "登录已过期", null);
            }
            activityDO.setUserId(loginUser.getId());
        }else if(token.startsWith("orguser_")){
            // 获取登录用户
            OrgUserDO orgUserDO = super.getOrgLoginUser(token);
            if (orgUserDO == null ) { // 判断是否登录
                return ResultDTOBuilder.failure("0000001", "登录已过期", null);
            }
            activityDO.setUserId(orgUserDO.getCreateUserId());
        }
        return activityService.deleteActivity(activityDO);
    }

    // 活动报名
    @PostMapping("/signActivity")
    public ResultDTO signActivity (HttpServletRequest request, @RequestBody ActivityDO activityDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        activityDO.setUserId(loginUser.getId());
        return activityService.signActivity(activityDO);
    }

    // 取消报名活动
    @PostMapping("/cancleSignActivity")
    public ResultDTO cancleSignActivity (HttpServletRequest request, @RequestBody ActivityDO activityDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        activityDO.setUserId(loginUser.getId());
        return activityService.cancleSignActivity(activityDO);
    }

    // 查询活动详细信息
    @PostMapping("/queryActivityDetail")
    public ResultDTO queryActivityDetail (HttpServletRequest request, @RequestBody ActivityDO activityDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        if(token.startsWith("user_")) {
            LoginUser loginUser = super.getLoginUser(token);
            if (loginUser == null) { // 判断是否登录
                return ResultDTOBuilder.failure("0000001", "登录已过期", null);
            }
            activityDO.setUserId(loginUser.getId());
        }else if(token.startsWith("orguser_")){
            // 获取登录用户
            OrgUserDO orgUserDO = super.getOrgLoginUser(token);
            if (orgUserDO == null ) { // 判断是否登录
                return ResultDTOBuilder.failure("0000001", "登录已过期", null);
            }
            activityDO.setUserId(orgUserDO.getCreateUserId());
        }
        return activityService.queryActivityDetail(activityDO);
    }

    // 查询活动报名列表
    @PostMapping("/queryActivitySignList")
    public ResultDTO queryActivitySignList (HttpServletRequest request, @RequestBody QueryParamBO queryParamBO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        queryParamBO.setUserId(loginUser.getId());
        return activityService.queryActivitySignList(queryParamBO, getPageQuery(queryParamBO.getPageNo(), queryParamBO.getPageSize()));
    }

    // 活动评论上传图片
    @PostMapping("/uploadActivityCommentUrl")
    public ResultDTO uploadActivityCommentUrl (HttpServletRequest request, MultipartFile[] lstFile) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        return activityService.uploadFile(lstFile, loginUser.getId(), "activityCommentUrl");
    }

    // 添加活动评论
    @PostMapping("/addActivityComment")
    public ResultDTO addActivityComment (HttpServletRequest request, @RequestBody ActivityCommentDO activityCommentDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        activityCommentDO.setCommentUserId(loginUser.getId());
        return activityService.addActivityComment(activityCommentDO);
    }

    // 删除活动评论
    @PostMapping("/deleteActivityComment")
    public ResultDTO deleteActivityComment (HttpServletRequest request, @RequestBody ActivityCommentDO activityCommentDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        activityCommentDO.setCommentUserId(loginUser.getId());
        return activityService.deleteActivityComment(activityCommentDO);
    }

    // 点赞活动评论
    @PostMapping("/addActivityCommentHeart")
    public ResultDTO addActivityCommentHeart (HttpServletRequest request, @RequestBody ActivityCommentHeartDO activityCommentHeartDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        activityCommentHeartDO.setUserId(loginUser.getId());
        return activityService.addActivityCommentHeart(activityCommentHeartDO);
    }

    // 取消活动评论点赞
    @PostMapping("/cancleActivityCommentHeart")
    public ResultDTO cancleActivityCommentHeart (HttpServletRequest request, @RequestBody ActivityCommentHeartDO activityCommentHeartDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        activityCommentHeartDO.setUserId(loginUser.getId());
        return activityService.cancleActivityCommentHeart(activityCommentHeartDO);
    }

    // 查询活动评论列表
    @PostMapping("/queryActivityCommentList")
    public ResultDTO queryActivityCommentList (HttpServletRequest request, @RequestBody QueryParamBO queryParamBO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        queryParamBO.setUserId(loginUser.getId());
        return activityService.queryActivityCommentList(queryParamBO);
    }

    // 查询活动推荐列表
    @PostMapping("/queryActivityRecommendList")
    public ResultDTO queryActivityRecommendList (HttpServletRequest request, @RequestBody QueryParamBO queryParamBO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        queryParamBO.setUserId(loginUser.getId());
        return activityService.queryActivityRecommendList(queryParamBO, getPageQuery(queryParamBO.getPageNo(), queryParamBO.getPageSize()));
    }

    // 查询活动列表
    @PostMapping("/queryActivityList")
    public ResultDTO queryActivityList (HttpServletRequest request, @RequestBody QueryParamBO queryParamBO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        if(token.startsWith("user_")) {
            LoginUser loginUser = super.getLoginUser(token);
            if (loginUser == null) { // 判断是否登录
                return ResultDTOBuilder.failure("0000001", "登录已过期", null);
            }
            queryParamBO.setUserId(loginUser.getId());
        }else if(token.startsWith("orguser_")){
            // 获取登录用户
            OrgUserDO orgUserDO = super.getOrgLoginUser(token);
            if (orgUserDO == null ) { // 判断是否登录
                return ResultDTOBuilder.failure("0000001", "登录已过期", null);
            }
            queryParamBO.setUserId(orgUserDO.getCreateUserId());
        }
        return activityService.queryActivityList(queryParamBO, getPageQuery(queryParamBO.getPageNo(), queryParamBO.getPageSize()));
    }

    // 查询我的活动列表
    @PostMapping("/queryUserActivityList")
    public ResultDTO queryUserActivityList (HttpServletRequest request, @RequestBody QueryParamBO queryParamBO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        queryParamBO.setUserId(loginUser.getId());
        queryParamBO.setIfSelf(1);
        return activityService.queryActivityList(queryParamBO, getPageQuery(queryParamBO.getPageNo(), queryParamBO.getPageSize()));
    }
}
